package com.mytrip.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class PhotoUtil{
	/**
	 * 给图片加水印，但不改变大小
	 * 
	 * @param strOriginalFileName
	 *        String(原始文件)
	 * @param strWaterMarkFileName
	 *        String(水印后)
	 */
	public static void waterMark(String strOriginalFileName, String strWaterMarkFileName, HttpSession session){
		try {
			// 源文件
			String root = session.getServletContext().getRealPath("/");
			File fileOriginal = new File(root + strOriginalFileName);
			Image imageOriginal = ImageIO.read(fileOriginal);
			int widthOriginal = imageOriginal.getWidth(null);
			int heightOriginal = imageOriginal.getHeight(null);
			System.out.println("widthOriginal:" + widthOriginal + "theightOriginal:" + heightOriginal);

			BufferedImage bufImage = new BufferedImage(widthOriginal, heightOriginal, BufferedImage.TYPE_INT_RGB);
			Graphics g = bufImage.createGraphics();
			g.drawImage(imageOriginal, 0, 0, widthOriginal, heightOriginal, null);

			// 水印文件
			File fileWaterMark = new File(root + strWaterMarkFileName);
			Image imageWaterMark = ImageIO.read(fileWaterMark);
			int widthWaterMark = imageWaterMark.getWidth(null);
			int heightWaterMark = imageWaterMark.getHeight(null);
			System.out.println("widthWaterMark:" + widthWaterMark + "theightWaterMark:" + heightWaterMark);

			// 水印文件在源文件的右下角
			g.drawImage(imageWaterMark, widthOriginal - widthWaterMark, heightOriginal - heightWaterMark,
					widthWaterMark, heightWaterMark, null);
			g.dispose();

			FileOutputStream fos = new FileOutputStream(root + strOriginalFileName);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
			encoder.encode(bufImage);
			fos.flush();
			fos.close();
			fos = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改图片大小
	 */
	public static void alterSize(String srcImgFile, String addChar, int new_w, int new_h){

		java.io.File file = new java.io.File(srcImgFile);
		if (file.exists())
			System.out.println("文件存在");
		else
			System.out.println("文件不存在");

		int i = srcImgFile.indexOf(".");
		int sLen = srcImgFile.length();
		String suffix = srcImgFile.substring(i, sLen); // 带点+后缀名
		String urlName = srcImgFile.substring(0, i); // 路径+文件名
		String newUrlName = urlName + addChar + suffix;
		System.out.print("新文件名为" + newUrlName);
		Image src = null;
		try {
			src = javax.imageio.ImageIO.read(file);
			java.awt.image.BufferedImage tag = new java.awt.image.BufferedImage(new_w, new_h,
					java.awt.image.BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().setColor(Color.WHITE);
			tag.getGraphics().fillRect(0, 0, new_w, new_h);
			tag.getGraphics().drawImage(src, 20, 20, new_w, new_h, null);
			FileOutputStream newimage = new FileOutputStream(newUrlName);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
			encoder.encode(tag); // 近JPEG编码
			newimage.close();

		} catch (IIOException e) {
			e.printStackTrace();
			System.out.print("这里出错了");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}// End sizeAlter

	public static void alterSize(InputStream inputStream, OutputStream outputStream, int new_w, int new_h){
		Image src = null;
		try {
			src = javax.imageio.ImageIO.read(inputStream);
			java.awt.image.BufferedImage tag = new java.awt.image.BufferedImage(new_w, new_h,
					java.awt.image.BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null);
			BufferedOutputStream bos = new BufferedOutputStream(outputStream);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
			encoder.encode(tag); // 近JPEG编码
			outputStream.close();
			bos.close();

		} catch (IIOException e) {
			e.printStackTrace();
			System.out.print("这里出错了");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void alterSize(InputStream inputStream, OutputStream outputStream, int maxWidthHeight){
		Image src = null;
		try {
			src = javax.imageio.ImageIO.read(inputStream);
			int o_height = src.getHeight(null);
			int o_width = src.getWidth(null);
			boolean t = o_width >= o_height;
			float scale = 0;
			int _x = 0;
			int _y = 0;
			int n_width = 0;
			int n_height = 0;
			if (t) {
				scale = (float)o_height / o_width;
				n_width = maxWidthHeight;
				n_height = Math.round(scale * maxWidthHeight);
				_x = 0;
				_y = Math.round((maxWidthHeight - n_height) / 2);
			} else {
				scale = (float)o_width / o_height;
				n_width = Math.round(scale * maxWidthHeight);
				n_height = maxWidthHeight;
				_x = Math.round((maxWidthHeight - n_width) / 2);
				_y = 0;
			}

			java.awt.image.BufferedImage tag = new java.awt.image.BufferedImage(maxWidthHeight, maxWidthHeight,
					java.awt.image.BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().setColor(Color.WHITE);
			tag.getGraphics().fillRect(0, 0, maxWidthHeight, maxWidthHeight);
			tag.getGraphics().drawImage(src, _x, _y, n_width, n_height, null);
			BufferedOutputStream bos = new BufferedOutputStream(outputStream);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
			encoder.encode(tag); // 近JPEG编码
			outputStream.close();
			bos.close();

		} catch (IIOException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

//	public static void main(String[] args){
//		PhotoUtil.alterSize("F:/a.jpg", "_", 100, 100);
//	}
}
